<template>
  <div
    class="modal fade"
    tabindex="-1"
    role="dialog"
    aria-hidden="true"
    data-bs-backdrop="static"
    :id="id"
  >
    <div class="modal-dialog modal-dialog-centered">
      <div class="modal-content">
        <div class="modal-header">
          <h5 class="modal-title">
            <slot name="title"> 更改客户 </slot>
          </h5>
          <button
            type="button"
            class="btn-close"
            data-bs-dismiss="modal"
            aria-label="Close"
          ></button>
        </div>
        <div class="modal-body">
          <form role="form" class="form-horizontal">
            <div class="row mb-0 mt-1">
              <label class="col-3 control-label text-end">客户</label>
              <div class="col-9" v-if="canShowSelect2 === 1">
                <my-select-customer
                  :customerId.sync="customerId"
                  :minId="0"
                  :dropdownParentId="id"
                ></my-select-customer>
              </div>
            </div>
            <div class="row mb-0 mt-1" v-if="costCenters.length > 0">
              <label class="col-3 control-label text-end">成本中心</label>
              <div class="col-9">
                <select class="form-select" v-model="costCenter">
                  <option value="">请选择</option>
                  <option :value="info.name" v-for="info in costCenters">
                    {{ info.name }}
                  </option>
                </select>
              </div>
            </div>
            <div class="row mb-0 mt-1" v-if="projectNames.length > 0">
              <label class="col-3 control-label text-end">项目名称</label>
              <div class="col-9">
                <select class="form-select" v-model="projectName">
                  <option value="">请选择</option>
                  <option :value="info.name" v-for="info in projectNames">
                    {{ info.name }}
                  </option>
                </select>
              </div>
            </div>
          </form>
        </div>
        <div class="modal-footer">
          <button
            type="button"
            class="btn btn-secondary me-2"
            data-bs-dismiss="modal"
          >
            关闭
          </button>
          <button type="button" class="btn btn-primary" @click.stop="ok()">
            确定
          </button>
        </div>
      </div>
    </div>
  </div>
</template>

<script>
import $ from "jquery";
import MySelectCustomer from "@/components/my-select2-customer.vue";
import { searchNotesByCustomer } from "@/api/customer.js";

export default {
  components: {
    MySelectCustomer,
  },
  props: {
    id: {
      type: String,
      default: "updateCustomerModal",
    },
  },
  data() {
    return {
      resolve: "",
      reject: "",
      promise: "",

      customerId: 0,
      costCenter: "",
      projectName: "",
      costCenters: [],
      projectNames: [],

      canShowSelect2: 0,

      myModal: null,
    };
  },
  watch: {
    customerId: function (newVal) {
      this.getCostCenters();
    },
  },
  methods: {
    ok: function () {
      this.hideModal()

      this.resolve({
        customerId: this.customerId,
        costCenter: this.costCenter,
        projectName: this.projectName,
      });
    },
    modal: function (customerId) {

      this.customerId = customerId;

    this.showModal();


      this.promise = new Promise((resolve, reject) => {
        this.resolve = resolve;
        this.reject = reject;
      });
      return this.promise;
    },
    getCostCenters: function () {
      this.costCenter = "";
      this.projectName = "";
      this.costCenters.splice(0);
      this.projectNames.splice(0);
      if (this.customerId < 1) return;

      searchNotesByCustomer(this.customerId, (jsonResult) => {
        for (const info of jsonResult) {
          if (info.noteType === 100) {
            this.costCenters.push(info);
          } else if (info.noteType === 200) {
            this.projectNames.push(info);
          }
        }
      });
    },
    showModal: function () {
      this.getMyModal().show();
    },
    hideModal: function () {
      this.getMyModal().hide();
    },
    getMyModal: function () {
      if (this.myModal === null) {
        let myModalEl = $(this.$el).get(0);
        this.myModal = new bootstrap.Modal(myModalEl, {
          keyboard: true,
        });

      const self = this;
      myModalEl.addEventListener("shown.bs.modal", function () {
        // 需要等bootstrap的对话框显示完成了，再渲染select2封装的控件
        console.log("shown.bs.modal")
        self.canShowSelect2 = 1;
      });

      }

      return this.myModal;
    },
  },
};
</script>
